//
//  ByShopCarCell.m
//  hutouben_ios
//
//  Created by pro on 16/12/1.
//  Copyright © 2016年 mc. All rights reserved.
//

#import "ByShopCarCell.h"

@implementation ByShopCarCell
{

    UIButton    *_selectBtn;            // 选中视图
    UIImageView *_mainView;             // 主图
    UILabel     *_titleLabel;           // 标题
    UILabel     *_skuLabel;             // 描述
    UILabel     *_priceLabel;           // 价格
    UIButton    *_deleteBtn;            // 删除视图
    
    UILabel     *_countLabel;           // 数量视图（可修改）
    UIButton    *_countAddBtn;          // 数量视图（+）
    UIButton    *_countDeleteBtn;       // 数量视图（-）

    
    
    UILabel     *_line;                 // 线
    UILabel     *_freightTitleLabel;    // 运费标题
    UILabel     *_freightPriceLabel;    // 运费价格


}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    
    
    // 选中视图
    _selectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_selectBtn setImage:[UIImage imageNamed:@"shopcar_weixuanzhong"] forState:UIControlStateNormal];
    [_selectBtn setImage:[UIImage imageNamed:@"shopcar_xuanzhong"] forState:UIControlStateSelected];
    [_selectBtn addTarget:self action:@selector(selectClick) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_selectBtn];
    
    // 主图
    _mainView = [[UIImageView alloc] init];
    [self.contentView addSubview:_mainView];
    
    // 标题
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.numberOfLines = 2;
    _titleLabel.font = FONT(14);
    _titleLabel.textColor = TEXT_COLOR;
    _titleLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:_titleLabel];
    
    // 描述
    _skuLabel = [[UILabel alloc] init];
    _skuLabel.adjustsFontSizeToFitWidth = YES;
    _skuLabel.font = FONT(13);
    _skuLabel.textColor = [UIColor grayColor];
    _skuLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:_skuLabel];
    
    // 价格
    _priceLabel = [[UILabel alloc] init];
    _priceLabel.font = FONT(18);
    _priceLabel.textColor = HTB_THEME_COLOR;
    _priceLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:_priceLabel];
    
    // 删除按钮
    _deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_deleteBtn setImage:[UIImage imageNamed:@"shopcar_weixuanzhong"] forState:UIControlStateNormal];
    [_deleteBtn addTarget:self action:@selector(deleteClick) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_deleteBtn];
    
    // 创建数量视图
    _countLabel = [[UILabel alloc] init];
    _countLabel.userInteractionEnabled = YES;
    _countLabel.font = FONT(14);
    _countLabel.text = @"1";
    _countLabel.textColor = [UIColor blackColor];
    _countLabel.textAlignment = NSTextAlignmentCenter;
    _countLabel.layer.cornerRadius = 4;
    _countLabel.layer.borderColor = ZJ_SEPLINE_COLOR.CGColor;
    _countLabel.layer.borderWidth = 1;
    [self.contentView addSubview:_countLabel];
    
    _countDeleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _countDeleteBtn.titleLabel.font = B_FONT(25);
    [_countDeleteBtn setTitle:@"-" forState:UIControlStateNormal];
    [_countDeleteBtn setTitleColor:HTB_THEME_COLOR forState:UIControlStateNormal];
    [_countDeleteBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateDisabled];
    [_countDeleteBtn addTarget:self action:@selector(countDelete) forControlEvents:UIControlEventTouchUpInside];
    [_countLabel addSubview:_countDeleteBtn];
    

    _countAddBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _countAddBtn.titleLabel.font = B_FONT(25);
    [_countAddBtn setTitle:@"+" forState:UIControlStateNormal];
    [_countAddBtn setTitleColor:HTB_THEME_COLOR forState:UIControlStateNormal];
    [_countAddBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateDisabled];
    [_countAddBtn addTarget:self action:@selector(countAdd) forControlEvents:UIControlEventTouchUpInside];
    [_countLabel addSubview:_countAddBtn];

    
    // 线
    _line = [[UILabel alloc] init];
    _line.backgroundColor = ZJ_SEPLINE_COLOR;
    [self.contentView addSubview:_line];
    
    // 运费
    _freightTitleLabel = [[UILabel alloc] init];
    _freightTitleLabel.font = FONT(15);
    _freightTitleLabel.text = @"运费：";
    _freightTitleLabel.textColor = TEXT_COLOR;
    _freightTitleLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:_freightTitleLabel];
    
    _freightPriceLabel = [[UILabel alloc] init];
    _freightPriceLabel.font = FONT(18);
    _freightPriceLabel.textColor = HTB_THEME_COLOR;
    _freightPriceLabel.textAlignment = NSTextAlignmentRight;
    [self.contentView addSubview:_freightPriceLabel];
}


- (void)setSelect:(BOOL)select {
    
    _select = select;
    
    _selectBtn.selected = _select;
}
- (void)setCount:(NSNumber *)count {
    
    _count = count;
    
    _countLabel.text = [NSString stringWithFormat:@"%@", _count];
}

- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    
    _selectBtn.frame    = CGRectMake(0, 0, 40, 100);
    [_selectBtn setImageEdgeInsets:UIEdgeInsetsMake(40, 10, 40, 10)];
    
    _mainView.frame     = CGRectMake(_selectBtn.right, 10, 80, 80);
    _titleLabel.frame   = CGRectMake(_mainView.right + 10, 10, self.width - 10 - (_mainView.right + 10), 35);
    _skuLabel.frame     = CGRectMake(_titleLabel.left, _titleLabel.bottom, _titleLabel.width, 25);
    _priceLabel.frame   = CGRectMake(_skuLabel.left, _skuLabel.bottom, 100, 25);
    _deleteBtn.frame    = CGRectMake(0, 0, 20, 20);
    _deleteBtn.right    = _titleLabel.right;
    _deleteBtn.y        = _priceLabel.y;
    _countLabel.frame   = CGRectMake(0, _priceLabel.top, 0, 25);
    _countDeleteBtn.frame = CGRectMake(0, 0, _countLabel.height, _countLabel.height);
    _countAddBtn.frame  = CGRectMake(0, 0, _countLabel.height, _countLabel.height);

    _line.frame         = CGRectMake(0, _selectBtn.bottom, SCREEN_WIDTH, 1);
    _freightTitleLabel.frame = CGRectMake(10, _line.bottom, SCREEN_WIDTH / 2, self.height - _line.bottom);
    _freightPriceLabel.frame = CGRectMake(0, _line.bottom, SCREEN_WIDTH / 2, _freightTitleLabel.height);
    _freightPriceLabel.right = SCREEN_WIDTH - 10;
    
    _mainView.backgroundColor = [UIColor orangeColor];
    _titleLabel.text    = @"【商品种类】这里是个商品标题，自适应2行，多余的用...显示";
    _skuLabel.text      = @"规格1，规格2";
    _priceLabel.text    = @"￥744";
    _priceLabel.width   = [_priceLabel.text getSizeOfString:18].width + 10;
    _countDeleteBtn.enabled = _countLabel.text.integerValue != 1;

    CGFloat maxWidth    = 100;
    CGFloat avilWidth   = _deleteBtn.left - 10 - (_priceLabel.right + 10);
    _countLabel.width   = MIN(avilWidth, maxWidth);
    _countLabel.right   = _deleteBtn.left - 10;
    _countAddBtn.right  = _countLabel.width;
    
    _freightPriceLabel.text = @"￥777";
}




#pragma mark - 点击事件
- (void)selectClick {
    
    if (self.selectAction) {
        self.selectAction();
    }
}
- (void)deleteClick {
    if (self.deleteAction) {
        self.deleteAction();
    }
}
- (void)countAdd {
    
    NSString *countStr = _countLabel.text;
    NSInteger count = countStr.integerValue;
    NSString *newCountStr = [NSString stringWithFormat:@"%li", ++count];
    _countLabel.text = newCountStr;
    
    _countDeleteBtn.enabled = (count != 1);
    
    if (self.modifyCountAction) {
        self.modifyCountAction(count);
    }
}
- (void)countDelete {
    
    NSString *countStr = _countLabel.text;
    NSInteger count = countStr.integerValue;
    NSString *newCountStr = [NSString stringWithFormat:@"%li", --count];
    _countLabel.text = newCountStr;
    
    _countDeleteBtn.enabled = (count != 1);
    
    if (self.modifyCountAction) {
        self.modifyCountAction(count);
    }
}


@end
